package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.SysRole;
import com.qf.entity.SysUser;
import com.qf.entity.SysUserVO;
import com.qf.entity.UserDTO;
import com.qf.mapper.UserMapper;
import com.qf.service.UserService;
import com.qf.service.impl.UserDetailsService;
import com.qf.utils.JwtUtils;
import com.qf.utils.R;
import com.qf.utils.RedisUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/user/login")
    public R login(@RequestBody SysUser sysUser) {
        System.out.println("login:" + sysUser.getUsername() + " " + sysUser.getPassword());
        String jwtToken = userService.login(sysUser.getUsername(), sysUser.getPassword());
        System.out.println("jwtToken:" + jwtToken);
        return R.success(jwtToken);

    }


/*    @PostMapping("/user/login")
    public R userLogin(@RequestBody SysUser sysUser){
        System.out.println("user = " + sysUser);
        SysUser sysUser1 = userService.userLogin(sysUser);
        if (sysUser1 != null){
            return R.success(sysUser1);
        }else {
            return R.fail();
        }
    }*/

/*    *//**
     * 使用分页查全部
     * pageNum 当前页码
     * pageSize 页面大小
     * 这两个参数需要前端发请求带过来
     *//*
    @GetMapping("/user/searchV1")
    public R testMybatis2(int pageNum,int pageSize) {
        // 使用步骤
        // 1 先设置分页信息
        PageHelper.startPage(pageNum,pageSize);
        // 2 正常执行查询
        List<User> list = userMapper.findAll();
        // 3 通过查询返回的list创建出分页信息,PageInfo内包含所有分页数据,可以点入源码查看
        PageInfo<User> info = new PageInfo<>(list);
        System.out.println("当前面"+info.getPageNum());
        System.out.println("页面大小"+info.getPageSize() );
        System.out.println("总条数"+info.getTotal() );
        System.out.println("总页数"+info.getPages() );
        System.out.println("数据"+info.getList());
        return R.success(info);
    }*/

    @GetMapping("/user/list")
    public R secherUserList(UserDTO userDTO) {
        System.out.println(userDTO);
        // 使用步骤
        // 1 先设置分页信息
        PageHelper.startPage(userDTO.getPageNum(),userDTO.getPageSize());
        // 2 正常执行查询
        //查询用户表
        List<SysUserVO> userlist = userMapper.findAll(userDTO);

        //存储角色集合
        List<List<SysRole>> rolesList = new ArrayList<>();
        //查询用户对应的角色表
        userlist.forEach(user -> {
            //根据用户Id查找角色信息
            Integer userId = user.getId();
            //System.out.println(userId);
            List<SysRole> roleList = userMapper.findRoleByUserId(userId);
            //System.out.println(roleList);
            rolesList.add(roleList);
            user.setRoles(roleList);
        });
        //System.out.println(userlist);



        // 3 通过查询返回的list创建出分页信息,PageInfo内包含所有分页数据,可以点入源码查看
        PageInfo<SysUser> info = new PageInfo<>(userlist);
//        System.out.println("当前面"+info.getPageNum());
//        System.out.println("页面大小"+info.getPageSize() );
//        System.out.println("总条数"+info.getTotal() );
//        System.out.println("总页数"+info.getPages() );
//        System.out.println("数据"+info.getList());
        return R.success(info);
    }
    @GetMapping("/user/delete/{id}")
    public R deleteUser(@PathVariable int id){
        int i = userService.deleteUser(id);
        if (i > 0){
            return R.success(id);
        }else {
            return R.fail();
        }
    }
    @PostMapping("/user/delete/all")
    public R deleteUserAll(@RequestBody List<Integer> ids){
        int i = userService.deleteAll(ids);
        if (i > 0){
            return R.success(ids);
        }else {
            return R.fail();
        }
    }

    @PostMapping("/user/update")
    public R updateUser(@RequestBody SysUserVO sysUserVO){
        System.out.println(sysUserVO.getRoles());
        int i = userService.updateUser(sysUserVO);
        if (i > 0){
            return R.success(i);
        }else {
            return R.fail();
        }
    }

    @PostMapping("/user/addUser")
    public R addUser(@RequestBody SysUser sysUser){
        int i = userService.addUser(sysUser);
        if (i > 0){
            return R.success(sysUser);
        }else {
            return R.fail();
        }
        }


    /**
     * 系统用户退出
     */
    @PostMapping("/user/logout")
    public R logout(@RequestBody Map<String,String> map) {
        String token = map.get("token");
        Claims claims = jwtUtils.parseToken(token);
        Object uuid = claims.get("uuid");
        // 清除redis中的令牌
        redisUtil.deleteObject("user:key:"+uuid);
        return R.success( );
    }


    }
